package q;

import extlib.Pair;
import static extlib.Pair.*;

public class R implements Group {

    private final Pair<Integer, Integer> r;
    private int N = (int) Math.sqrt(Console.n);

    private R(Pair<Integer, Integer> r) {
        this.r = r;
    }

    @Override
    public R times(R rr) {
        if ((x() <= N) && (y() <= N) && (rr.x() <= N) && (rr.y() <= N)) {
            return R.R(x() * rr.x(), y() * rr.y());
        } else {
            return R.R(1, 1);
        }
    }

    @Override
    public R inverse() {
        if ((x() <= N) && (y() <= N)) {
            return R.R(y(), x());
        } else {
            return R.R(1, 1);
        }
    }

    public Integer x() {
        return r.x();
    }

    public Integer y() {
        return r.y();
    }

    public Pair<Integer, Integer> r() {
        return r;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final R other = (R) obj;
        if (this.r != other.r && (this.r == null || !this.r.equals(other.r))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 59 * hash + (this.r != null ? this.r.hashCode() : 0);
        return hash;
    }

    @Override
    public String toString() {
        return "(" + r.x() + "," + r.y() + ")";
    }

    public static R R(Integer x, Integer y) {
        return new R(pair(x, y));
    }
}
